
#include <BFile>
#include <BXmlDocument>
#include "Config.h"

Config::Config()
{
	_dirty = false;
	_language = "english";
	_targetSpaceVisual = true;
	_nodeSpaceVisual = true;
	_shapeBoxVisual = true;
}
Config::~Config()
{

}

bool Config::load()
{
	BString fileName = this->name();
	BFile file(fileName);
	if (!file.open(IO_Read))
	{
		return false;
	}

	BString errorMsg;
	BXmlDocument xmldoc;
	BXmlNode* root = xmldoc.read(file);
	if (!root)
	{
		return false;
	}
	if (BXmlNode* languageNode = root->child("language"))
	{
		_language = languageNode->text();
	}
	_dirty = false;
	return true;
}
bool Config::save()
{
	BString fileName = this->name();
	BFile file(fileName);
	if (!file.open(IO_Write))
	{
		return false;
	}

	BXmlDocument xmldoc;
	xmldoc.setFormat("/>\n");
	BXmlNode* root = new BXmlNode("config");
	root->setFormat(">\n</>\n");
	if (BXmlNode* languageNode = root->addChild("language"))
	{
		languageNode->setText(_language);
	}
	bool res = xmldoc.write(file, root);
	int len = file.size();

	_dirty = false;
	return true;
}
bool Config::isDirty() const
{
	return _dirty;
}

void Config::setLanguage(const BString& language)
{
	if (_language != language)
	{
		_language = language;
		_dirty = true;
	}
}
const BString& Config::language() const
{
	return _language;
}

